package br.ufrgs.ga.model;

import br.ufrgs.ga.fitness.FitnessCalculator;

public class Individual implements Comparable<Individual> {

	private final FitnessCalculator fitnessCalculator;
	private final IChromosome<String> chromosome;
	
	public Individual(IChromosome<String> chromosome, FitnessCalculator fitnessCalculator) {
		this.chromosome = chromosome;
		this.fitnessCalculator = fitnessCalculator;
	}
	
	public IChromosome<String> getChromosome() {
		return chromosome;
	}
		
	public Integer getFitnessValue() {
		return (Integer)fitnessCalculator.calculate(chromosome.getDnaStrand());
	}

	@Override
	public int compareTo(Individual o) {
		if (this.getFitnessValue() > o.getFitnessValue())
			return 1;
		else if (this.getFitnessValue() < o.getFitnessValue())
			return -1;
		else
			return 0;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		
		if (this == obj) {
			return true;
			
		} else if (obj instanceof Individual) {
			Individual individual = (Individual)obj;
			
			if (this.getChromosome().getDnaStrand().equals(individual.getChromosome().getDnaStrand()) &&
				this.getFitnessValue().equals(individual.getFitnessValue())) {
				return true;
			}
			
			return false;
			
		} else {
			return false;
		}
	}
	
	@Override
	public String toString() {
		return this.getChromosome() + " (" + this.getFitnessValue() + ")";
	}
}
